﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class ExamineRepository : RepositoryBase<Examine>
    {
        public IQueryable<ProductTechnologyView> GetList(int status,string productName, DateTime beginTime, DateTime endTime)
        {
            //连接采购
            var result = from exa in dbcontext.Examines
                         join pur in dbcontext.PurchaseComposes
                         on exa.ItemId equals pur.Id
                         //join op in dbcontext.OrderProducts
                         //on exa.ItemId equals op.PurchaseId
                         join pro in dbcontext.Products
                         on pur.ProductId equals pro.Id
                         join u in dbcontext.Users
                         on exa.ApplyId equals u.Id
                         where (productName == "" || pro.Name.Contains(productName))
                         && (pur.Type == 1)
                         && (exa.Status == status)
                         && (exa.ApplyTime >= beginTime && exa.ApplyTime <= endTime)
                         select new ProductTechnologyView
                         {
                             ExamineId = exa.Id,
                             ProductName = pro.Name,
                             //Type = pro.Type,
                             ExamineType = exa.Type,
                             ApplyName = u.RealName,
                             ApplyTime = exa.ApplyTime,
                             Amount = pur.Amount,
                             //Amount = pur.ArrivalAmount,
                             ECreateTime = exa.CreateTime,
                             Status = exa.Status,
                             StandardAmount = exa.StandardAmount
                         };
            //连接生产
            var result1 = from exa in dbcontext.Examines
                          join wor in dbcontext.Worksheets
                          on exa.ItemId equals wor.Id
                          //join op in dbcontext.OrderProducts
                          //on exa.ItemId equals op.WorksheetId
                          join pro in dbcontext.Products
                          on wor.ProductId equals pro.Id
                          join u in dbcontext.Users
                          on exa.ApplyId equals u.Id
                          where (productName == "" || pro.Name.Contains(productName))
                          && (exa.Status == status)
                          && (exa.ApplyTime >= beginTime && exa.ApplyTime <= endTime)
                          select new ProductTechnologyView
                          {
                              ExamineId = exa.Id,
                              ProductName = pro.Name,
                              //Type = pro.Type,
                              ExamineType = exa.Type,
                              ApplyName = u.RealName,
                              ApplyTime = exa.ApplyTime,
                              Amount = wor.WorkAmount,
                              ECreateTime = exa.CreateTime,
                              Status = exa.Status,
                              StandardAmount = exa.StandardAmount
                          };
            //自建采购单
            var result2 = from exa in dbcontext.Examines
                          join pur in dbcontext.PurchaseComposes
                          on exa.ItemId equals pur.Id
                          join u in dbcontext.Users
                          on exa.ApplyId equals u.Id
                          where (productName == "" || pur.ProductName.Contains(productName))
                          && (pur.Type == 2 || pur.Type == 3)
                          && (exa.Status == status)
                          && (exa.ApplyTime >= beginTime && exa.ApplyTime <= endTime)
                          select new ProductTechnologyView
                          {
                              ExamineId = exa.Id,
                              ProductName = pur.ProductName,
                              //Type = 1,
                              ExamineType = exa.Type,
                              ApplyName = u.RealName,
                              ApplyTime = exa.ApplyTime,
                              Amount = pur.Amount,
                              //Amount = pur.ArrivalAmount,
                              ECreateTime = exa.CreateTime,
                              Status = exa.Status,
                              StandardAmount = exa.StandardAmount
                          };

            return result.Concat(result1).Concat(result2);
        }

    }
}
